package fnic.prehand.augment.resource;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * XML -> this
 * 用xstream直接生成DBCP中BasicDataSource好像有点问题 ，所以采用折中的方法。。
 * @author Wu
 *
 */
public class DBPool {

	//dbcp基本配置参数
	public String name;
	public String driverClassName;
	public String url;
	public String username;
	public String password;
	public int initialSize;
	public int maxActive;
	public int maxIdle;
	public int maxWait;
	public boolean removeAbandoned;
	public int removeAbandonedTimeout;
	public boolean testOnBorrow;
	public boolean logAbandoned;
	//连接池
	private BasicDataSource bds;
	
	public DBPool() {

	}

	public void init() {
		bds = new BasicDataSource();
		bds.setDriverClassName(driverClassName);
		bds.setUrl(url);
		bds.setUsername(username);
		bds.setPassword(password);
		bds.setInitialSize(initialSize);
		bds.setMaxActive(maxActive);
		bds.setMaxIdle(maxIdle);
		bds.setMaxWait(maxWait);
		bds.setRemoveAbandoned(removeAbandoned);
		bds.setRemoveAbandonedTimeout(removeAbandonedTimeout);
		bds.setTestOnBorrow(testOnBorrow);
		bds.setLogAbandoned(logAbandoned);
	}
	
	public synchronized Connection getConnection() throws SQLException{
		if(bds != null)
		     return bds.getConnection();
		return null;
		
	}

	
}
